package com.privatework.common.service.leave.impl;

import com.privatework.common.QueryConditionVo;
import com.privatework.common.converter.LeaveInfoConverter;
import com.privatework.common.entity.leave.LeaveInfo;
import com.privatework.common.entity.leave.QLeaveInfo;
import com.privatework.common.repository.leave.LeaveInfoRepository;
import com.privatework.common.repository.user.UserRepository;
import com.privatework.common.requestvo.leave.LeaveInfoReqVo;
import com.privatework.common.service.leave.LeaveService;
import com.privatework.common.vo.leave.LeaveInfoResultVo;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author johnny
 * @create 2021-04-12 3:05 下午
 **/
@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private JPAQueryFactory queryFactory;

    @Autowired
    private LeaveInfoRepository leaveInfoRepository;

    @Autowired
    private UserRepository userRepository;


    @Override
    public Page<LeaveInfoResultVo> listByCondition(LeaveInfoReqVo leaveInfoReqVo, Pageable pageable) {
        List<LeaveInfoResultVo> leaveInfoVos = new ArrayList<>();
        QLeaveInfo qLeaveInfo = QLeaveInfo.leaveInfo;
        JPAQuery<LeaveInfo> jpaQuery = queryFactory.selectFrom(qLeaveInfo)
                .offset(pageable.getPageNumber() * pageable.getPageSize())
                .limit(pageable.getPageSize());

        fillCondition(jpaQuery, leaveInfoReqVo);

        QueryResults<LeaveInfo> queryResults = jpaQuery.fetchResults();
        queryResults.getResults().forEach(leaveInfo -> {
            LeaveInfoResultVo leaveInfoResultVo = LeaveInfoConverter.INSTANCE.domain2vo(leaveInfo);
            userRepository.findById(leaveInfoResultVo.getStudentUserId()).ifPresent(userEntity -> {
                leaveInfoResultVo.setPhone(userEntity.getPhone());
                leaveInfoResultVo.setGender(userEntity.getGender());
            });
            leaveInfoVos.add(leaveInfoResultVo);
        });
        return new PageImpl<>(leaveInfoVos, pageable, queryResults.getTotal());
    }

    @Override
    public void submitLeave(LeaveInfoReqVo leaveInfoReqVo) {
        Long id = leaveInfoReqVo.getId();
        leaveInfoRepository.findById(id).ifPresent(leaveInfo -> {
            leaveInfo.setStatus(leaveInfoReqVo.getStatus());
            leaveInfo.setRejectReason(leaveInfoReqVo.getRejectReason());
            leaveInfoRepository.save(leaveInfo);
        });
    }

    @Override
    public void submitaddLeave(LeaveInfoReqVo leaveInfoReqVo) {
        LeaveInfo leaveInfo = LeaveInfoConverter.INSTANCE.vo2domain(leaveInfoReqVo);
        leaveInfo.setStatus("未审批");
        leaveInfoRepository.save(leaveInfo);
    }


    @Override
    public void fillCondition(JPAQuery<LeaveInfo> jpaQuery, QueryConditionVo queryConditionVo) {
        QLeaveInfo qLeaveInfo = QLeaveInfo.leaveInfo;
        LeaveInfoReqVo leaveInfoReqVo = (LeaveInfoReqVo) queryConditionVo;
        if (StringUtils.isNotEmpty(leaveInfoReqVo.getStatus())) {
            jpaQuery.where(qLeaveInfo.status.eq(leaveInfoReqVo.getStatus()));
        }
        if (StringUtils.isNotEmpty(leaveInfoReqVo.getStudentName())) {
            jpaQuery.where(qLeaveInfo.studentName.like("%" + leaveInfoReqVo.getStudentName() + "%"));
        }
    }
}